package com.quo.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.quo.dto.ProductsDto;
import com.quo.dto.StockInfoDto;
import com.quo.entity.Emp;
import com.quo.entity.Product;
import com.quo.entity.ProductSeries;
import com.quo.entity.ProductsDtos;
import com.quo.entity.StockRecord;
import com.quo.service.ProductService;
import com.quo.service.StockService;
import com.quo.service.SystemService;
import com.quo.utils.ExcelWriter;
import com.quo.utils.Result;
import com.quo.utils.ResultCode;

/** 

* @author zhoumin

*/
@RestController("stockController")
@RequestMapping(value="/api")
public class StockController {
	
	    @Autowired
	    public ProductService pService;
	    
	    @Autowired
	    public StockService stockService;
	    
	    @Autowired
		public SystemService systemService;
	
	
	  //在库一览					
	  @GetMapping("/stock-list") 
	  public Result getStockList(){ 
		  
		  List<ProductsDto> stockList =	pService.getProductList(); 
		  Result result=new Result(ResultCode.SUCCESS);
		  result.setData(stockList);     
		  
		  return result;
	  
	  }
	  
	  
	  //出入库详情					
	  @GetMapping("/stock-detail-list")
	  public Result getStockInfoList(){ 
		  
		  List<StockInfoDto> stockInfoList = stockService.getStockInfoList(); 
		  Result result=new Result(ResultCode.SUCCESS);
		  result.setData(stockInfoList);     
		  
		  return result;
	  
	  }
	  
	  
	  //更新入库后的库存数量及出入库记录
	   @PostMapping("/stock-in/{pid}")
	   public Result updateStockInInfo(@PathVariable(value="pid") Long pid,@RequestBody StockInfoDto stockInfoDto,HttpServletRequest request){ 
	    
	    
	    System.out.println("pid"+pid+"quantity"+stockInfoDto.getQuantity());

	    Product product = pService.getProductByPid(pid);    
	    Integer preStock = product.getStock();
	    Integer stock = preStock + stockInfoDto.getQuantity();    
	    pService.updateStock(pid,stock);  
	    System.out.println("preStock"+preStock+"stock"+stock);
	        
	    Cookie[] cookies = request.getCookies();	
		  Integer eno = 0;
		  String pwd = null;
		  for(Cookie cookie : cookies) {
			  String name = cookie.getName();
			  String value = cookie.getValue();         	  		    
		    if(name.equals("a")) {
		    	String id = cookie.getValue(); 
		    	eno = Integer.valueOf(id);		       	
		    }		    
		    else if(name.equals("b")) {
		    	pwd = cookie.getValue();		    		    	
		    }			  	          
		  }	 
		  
		  System.out.println("eno:"+eno);
		  System.out.println("pwd:"+pwd);
		  Emp emp = systemService.ifExists(eno,pwd);
		  System.out.println("emp111111111"+emp);  
	    StockRecord stockRecord = new StockRecord();
	    stockRecord.setPid(pid);
	    stockRecord.setAtid(2);
	    stockRecord.setPreStock(preStock);
	    stockRecord.setQuantity(stockInfoDto.getQuantity());
	    stockRecord.setAfStock(stock);
	    stockRecord.setHeno(emp.getEno());    
	    SimpleDateFormat sdfy = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	    stockRecord.setHdate(sdfy.format(new Date()));
	  
	    stockService.addStockRecord(stockRecord);         
	    return new Result(ResultCode.SUCCESS);
	   
	   }
	  
	  
	  //更新出库后的库存数量及出入库记录
	  @PostMapping("/stock-out/{pid}") 
	  public Result updateStockOutInfo(@PathVariable(value="pid") Long pid,@RequestBody StockInfoDto stockInfoDto,HttpServletRequest request){ 
		  System.out.println("pid"+pid+"quantity"+stockInfoDto.getQuantity());

		  Product product = pService.getProductByPid(pid);		  
		  Integer preStock = product.getStock();
		  Integer stock = preStock - stockInfoDto.getQuantity();		  
		  pService.updateStock(pid,stock);		
		  System.out.println("preStock"+preStock+"stock"+stock);
		  		  
		  Cookie[] cookies = request.getCookies();	
		  Integer eno = 0;
		  String pwd = null;
		  for(Cookie cookie : cookies) {
			  String name = cookie.getName();
			  String value = cookie.getValue();         	  		    
		    if(name.equals("a")) {
		    	String id = cookie.getValue(); 
		    	eno = Integer.valueOf(id);		       	
		    }		    
		    else if(name.equals("b")) {
		    	pwd = cookie.getValue();		    		    	
		    }			  	          
		  }	 
		  
		  System.out.println("eno:"+eno);
		  System.out.println("pwd:"+pwd);
		  Emp emp = systemService.ifExists(eno,pwd);
		  System.out.println("emp111111111"+emp);
		  StockRecord stockRecord = new StockRecord();
		  stockRecord.setPid(pid);
		  stockRecord.setAtid(1);
		  stockRecord.setPreStock(preStock);
		  stockRecord.setQuantity(stockInfoDto.getQuantity());
		  stockRecord.setAfStock(stock);
		  stockRecord.setHeno(emp.getEno());			  
		  SimpleDateFormat sdfy = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");	
		  stockRecord.setHdate(sdfy.format(new Date()));
		  stockService.addStockRecord(stockRecord);     		  
		  return new Result(ResultCode.SUCCESS);
	  
	  }
		/**
		 * author:韩宛廷 库存信息导出
		 * 
		 */
	  @RequestMapping(value = "/stock-export", method = RequestMethod.POST)
		public void StockExport(@RequestBody Long[] pids, HttpServletRequest request,
				HttpServletResponse response) {
			String[] arrays = { "製品番号", "製品名", "タイプ" ,"シリーズ","単価","在庫数"};

			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition",
					"attachment;filename=Product-" + System.currentTimeMillis() + ".xlsx");
			
			List<ProductsDtos> sidList = stockService.getByHids(pids);
			System.out.println(sidList);
			ExcelWriter<ProductsDtos> ew = new ExcelWriter<>();
			XSSFWorkbook workbook = ew.getWorkbook(sidList, "库存信息", ProductsDtos.class, arrays);
			ServletOutputStream outputStream = null;
			try {
				outputStream = response.getOutputStream();
				workbook.write(outputStream);

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					workbook.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					outputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	  
	  
	  
	

}
